package com.ruoyi.web.controller.publish;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.publish.domain.RvSample;
import com.ruoyi.publish.service.IRvSampleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.publish.domain.RvReview;
import com.ruoyi.publish.service.IRvReviewService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审读稿件Controller
 *
 * @author confusefox
 * @date 2020-11-01
 */
@Controller
@RequestMapping("/publish/review")
public class RvReviewController extends BaseController {
    private String prefix = "publish/review";

    @Autowired
    private IRvReviewService rvReviewService;

    @Autowired
    private IRvSampleService rvSampleService;


    //    @RequiresPermissions("publish:review:view")
    @GetMapping()
    public String review() {
        return prefix + "/review";
    }

    /**
     * 查询审读稿件列表
     */
//    @RequiresPermissions("publish:review:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RvReview rvReview) {
        startPage();
        List<RvReview> list = rvReviewService.selectRvReviewList(rvReview);
        return getDataTable(list);
    }

    /**
     * 导出审读稿件列表
     */
//    @RequiresPermissions("publish:review:export")
    @Log(title = "审读稿件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RvReview rvReview) {
        List<RvReview> list = null;
        if (StringUtils.isNotEmpty(rvReview.getExportIds())) {
            list = rvReviewService.selectRecordByIds(rvReview.getExportIds().split(","));
        } else {
            list = rvReviewService.selectRvReviewList(rvReview);
        }
        ExcelUtil<RvReview> util = new ExcelUtil<RvReview>(RvReview.class);
        return util.exportExcel(list, "review");
    }

    /**
     * 新增审读稿件
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存审读稿件
     */
//    @RequiresPermissions("publish:review:add")
    @Log(title = "审读稿件", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RvReview rvReview) {
        rvReview.setCreateTime(DateUtils.getNowDate());
        return toAjax(rvReviewService.insertRvReview(rvReview));
    }

    /**
     * 修改审读稿件
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        RvReview rvReview = rvReviewService.selectRvReviewById(id);
        RvSample RvSample = rvSampleService.selectRvSampleById(rvReview.getSampleid());
        String status = RvSample.getName();
        mmap.put("rvReview", rvReview);
        mmap.put("status", status);

        return prefix + "/edit";
    }

    /**
     * 查看审读稿件
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        RvReview rvReview = rvReviewService.selectRvReviewById(id);
        RvSample RvSample = rvSampleService.selectRvSampleById(rvReview.getSampleid());
        String status = RvSample.getName();
        mmap.put("rvReview", rvReview);
        mmap.put("status", status);

        return prefix + "/detail";
    }

    /**
     * 修改保存审读稿件
     */
//    @RequiresPermissions("publish:review:edit")
    @Log(title = "审读稿件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RvReview rvReview) {
        return toAjax(rvReviewService.updateRvReview(rvReview));
    }

    /**
     * 删除审读稿件
     */
//    @RequiresPermissions("publish:review:remove")
    @Log(title = "审读稿件", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(rvReviewService.deleteRvReviewByIds(ids));
    }


    @GetMapping("/reviewUser/{userId}")
    public String reviewByUser(@PathVariable("userId") String userId, ModelMap mmap) {

        // 设置专家id
        mmap.addAttribute("expertId", userId);
        return prefix + "/reviewUser";
    }


    @PostMapping("/listByUser/{userId}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("userId") Long userId, RvReview rvReview) {
        startPage();
        // 设置专家Id
        rvReview.setExpertId(userId);
        List<RvReview> list = rvReviewService.selectRvReviewList(rvReview);
        return getDataTable(list);
    }


}
